<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '轮播图添加' : '轮播图编辑'"
      :visible.sync="obj.isShow"
      width="30%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <div class="box">
            <h2>+</h2>
            <input type="file" @change="changeImg1" v-if="obj.isShow" />
            <img :src="imgUrl" alt="" v-if="imgUrl" />
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="add()" v-if="obj.isAdd"
          >添加</el-button
        >
        <el-button type="primary" v-else @click="update()">编辑 </el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {bannerAdd,bannerInfo,bannerEdit} from "../../../request/api.js";
import {sucessAlert,warningAlert} from "../../../utils/alert.js"
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      form: {
        title: "",
        img: "",
        status: 1,
      },
      imgUrl: "",
    };
  },
  computed: {
    ...mapGetters({}),
  },
  methods: {
    ...mapActions({
       bannerasynclist: "banner/acyncChangeList",
    }),
    changeImg1(e) {
      // 图片的完整信息
      let file = e.target.files[0];
      console.log(file);
      // 验证图片大小限制
      // 1024B=1KB    1024KB=1MB   1024MB=1GB
      if (file.size > 2 * 1024 * 1024) {
        warningAlert("图片大小超出限制");
        return;
      }
      // 验证图片类型限制
      let imgType = [
        "image/jpeg",
        "image/jpg",
        "image/png",
        "image/webp",
        "image/gif",
      ];
      if (!imgType.includes(file.type)) {
        warningAlert("请上传规定类型图片");
        return;
      }
      // 根据图片的信息转换成具体的图片
      this.imgUrl = URL.createObjectURL(file);
      // 给form中的img字段赋值
      this.form.img = file;
    },
    //清空
    empty() {
      (this.form = {
         id: 0,
        title: "",
        img: "",
        status: 1,
      }),
        (this.imgUrl = "");
    },
    //取消
    cancel() {
      (this.obj.isShow = false), this.empty();
    },
    //添加
    add(){
     bannerAdd(this.form).then(res=>{
         console.log(res)
        if(res.data.code==200){
            sucessAlert(res.data.msg);
            this.cancel();
           this.bannerasynclist();
        }else{
            warningAlert(res.data.msg)
        }
       })
       },
      //获取一条数据的值
    getOne(id){
        bannerInfo({id:id}).then(res=>{
            if(res.data.code==200){
                this.form = res.data.list
                this.imgUrl = this.$pre + res.data.list.img
                this.form.id = id
            }
        })
    },
    //确认编辑
    update(){
         bannerEdit(this.form).then(res=>{
            if(res.data.code==200){
                sucessAlert(res.data.msg),
                this.cancel()
                //刷新列表数据
                this.bannerasynclist();
  
            }
         })
    }
  },
  mounted() {},
};
</script>
<style scoped>
.box {
  width: 150px;
  height: 150px;
  border: 1px dashed #ccc;
  position: relative;
}
.box h2 {
  font-size: 50px;
  line-height: 150px;
  margin: 0 auto;
  text-align: center;
}
.box input {
  width: 150px;
  height: 150px;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
  z-index: 3;
}
.box img {
  width: 150px;
  height: 150px;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
}
</style>